package com.hxc.controller;

import com.hxc.utils.AliOSSProperties;
import com.hxc.entity.Result;
import com.hxc.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSProperties aliOSSProperties;
//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.bucket}")
//    private String bucketName;
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        log.info("测试连接oss：{}，{}",aliOSSProperties.getEndpoint(),aliOSSProperties.getBucket());
        // 获取原始文件名，截取后缀
        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名：{}", originalFilename);
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("后缀：{}",extName);
        String src = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBucket(), file.getBytes(), extName);
        log.info("地址：{}",src);
        // 调用阿里云上传工具类上传文件
        return Result.success(src);
    }

}
